// ignore_for_file: prefer_typing_uninitialized_variables

/*
 * @Author: ZL zhangl@feelingtech.net
 * @Date: 2024-03-21 23:15:39
 * @LastEditors: ZL zhangl@feelingtech.net
 * @LastEditTime: 2024-06-18 22:33:26
 * @FilePath: \com.zy.fireprotection_app\lib\utils\dio_http.dart
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

import 'dart:convert';
import 'dart:math';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';

class DioHttp {
  var _client = Dio();
  var context;

  static const baseUrl = 'http://47.119.128.43:5005';

  static DioHttp of(BuildContext context) {
    return DioHttp._internal(context);
  }

  DioHttp._internal(BuildContext context) {
    if (context != this.context) {
      this.context = context;
      var options = BaseOptions(
          baseUrl: baseUrl,
          connectTimeout: const Duration(seconds: 10000),
          receiveTimeout: const Duration(seconds: 3000),
          extra: {
            "context": context,
          });
      var client = Dio(options);
      client.interceptors.add(InterceptorsWrapper(
        onRequest: (RequestOptions options, handler) {
          print('Request 请求: ${options.path}');
          print('Request 请求: ${options.data}');
          print('Request 请求: ${options.queryParameters}');
          print('Request Headers: ${options.headers}');
          return handler.next(options); // 继续请求
        },
        onResponse: (Response response, handler) {
          // 可选，在这里打印响应头
          // print('Response Headers: ${response.headers}')
          print('Response 响应: ${response.data}');
          return handler.next(response);
        },
      ));
      _client = client;
    }
  }

  get(String path, params, token) async {
    // print(params.runtimeType);
    var options = Options(headers: {
      'Authorization': 'Bearer ' + token,
      'Accept-Encoding': 'gzip, deflate',
      'Accept-Language': 'zh-CN,zh;q=0.9',
      'Content-Type': 'application/json;charset=UTF-8',
      'Content-Language': 'zh_CN',
    });
    return await _client.get(path, queryParameters: params, options: options);
  }

  Future<Response<Map<String, dynamic>>> post(String path,
      [Map<String, dynamic>? params, token]) async {
    var options = Options(headers: {
      'Authorization': 'Bearer ' + token,
      'Content-Type': 'application/json',
    });
    return await _client.post(path, data: params, options: options);
  }

  Future<Response<Map<String, dynamic>>> put(String path,
      [Map<String, dynamic>? params, token]) async {
    var options = Options(headers: {
      'Authorization': 'Bearer ' + token,
      'Content-Type': 'application/json',
    });
    return await _client.put(path, data: params, options: options);
  }

  Future<Response<Map<String, dynamic>>> postFormData(String path,
      [Map<String, dynamic>? params, token]) async {
    var options = Options(
      //f发送formdata
      headers: {
        'Authorization': 'Bearer ' + token,
        'Content-Type': 'multipart/form-data',
      },
    );
    return await _client.post(path,
        data: FormData.fromMap(params!), options: options);
  }
}
